package tongwentongshu.com.app.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.adapter.MyShareDetailsAdapter;
import tongwentongshu.com.app.adapter.NullAdapter;
import tongwentongshu.com.app.bean.ShareDetailsBean;
import tongwentongshu.com.app.contract.MyShareDetailsContract;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.dialog.ShareUtils;
import tongwentongshu.com.app.presenter.MyShareDetailsPresenter;
import tongwentongshu.com.app.utils.GlideUtil;

/**
 * Created by XiongBin on 2017/7/18.
 */

public class MyShareDetailsActivity extends BaseActivity implements MyShareDetailsContract.View, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.title_share)
    ImageView share;
    @BindView(R.id.rv_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private MyShareDetailsAdapter myShareDetailsAdapter;

    RecyclerView read_people;
    private NullAdapter nullAdapter;
    protected boolean onRefreshJudge = false;
    private MyShareDetailsPresenter myShareDetailsPresenter;
    private View baseView;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_my_share_details;
    }

    @Override
    protected int getTitleId() {
        return R.string.my_share_details;
    }

    @Override
    protected void initView() {

        share.setImageResource(R.drawable.home_share);
        share.setVisibility(View.VISIBLE);
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareUtils.initBooksDialog(mContext,
                        "看有力量的书籍，交有阅历的朋友",
                        Cache.getUser().getData().getShare_book() + msg.getData().getBookid(),
                        msg.getData().getImage(),
                        "您的好友分享了一本图书《" + msg.getData().getBook_name() + "》,快来阅读吧~");
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        nullAdapter = new NullAdapter();
        nullAdapter.openLoadAnimation();
        mRecyclerView.setAdapter(nullAdapter);

    }

    @Override
    protected void initData() {
        myShareDetailsPresenter = new MyShareDetailsPresenter(this);
        myShareDetailsPresenter.initShareDetails(getIntent().getExtras().getString("bookId"));
    }

    @Override
    public void getBasicView() {
        if (null == baseView) {
            baseView = getLayoutInflater().inflate(R.layout.view_share_details, (ViewGroup) mRecyclerView.getParent(), false);
            read_people = (RecyclerView) baseView.findViewById(R.id.ll_read_people);
            onRefreshJudge = true;
            mSwipeRefreshLayout.setOnRefreshListener(this);
            read_people.setLayoutManager(new LinearLayoutManager(mContext));
            List arr = new ArrayList<ShareDetailsBean.DataBean.PersoninfoBean>();
            myShareDetailsAdapter = new MyShareDetailsAdapter(mContext, arr);
            myShareDetailsAdapter.setOnLoadMoreListener(this, mRecyclerView);
            myShareDetailsAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
            View empty = getLayoutInflater().inflate(R.layout.empty_reader, (ViewGroup) mRecyclerView.getParent(), false);
            //pullToRefreshAdapter.setPreLoadNumber(3);
//            myShareDetailsAdapter.setEmptyView(empty);
            read_people.setAdapter(myShareDetailsAdapter);
            nullAdapter.addHeaderView(baseView);
            read_people.addOnItemTouchListener(new OnItemClickListener() {
                @Override
                public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
                    showUser(msg.getData().getPersoninfo().get(position).getPersonid());
                }
            });
        }
        TextView bookName = (TextView) baseView.findViewById(R.id.tv_book_name);
        bookName.setText(msg.getData().getBook_name());//图书名称
        TextView type = (TextView) baseView.findViewById(R.id.tv_book_type);
        type.setText("【" + msg.getData().getBook_typename() + "】");//图书类别
        TextView shareTime = (TextView) baseView.findViewById(R.id.tv_book_share_time);
        shareTime.setText("分享时间:" + msg.getData().getCreate_time());//分享时间
        TextView bookInfo = (TextView) baseView.findViewById(R.id.tv_book_info);
        bookInfo.setText("分享时间:" + msg.getData().getBook_introduce());//书简介
        TextView location = (TextView) baseView.findViewById(R.id.tv_location);
        location.setText(msg.getData().getBook_postition());//书定位
        TextView readerSum = (TextView) baseView.findViewById(R.id.tv_reader_sum);
        readerSum.setText(msg.getData().getPersoninfo().size() + "人读过:");//阅读人数
        ImageView bookPic = (ImageView) baseView.findViewById(R.id.iv_book_image);
        GlideUtil.setImage(mContext, msg.getData().getImage(), R.drawable.default_graph, R.drawable.default_graph, bookPic);

    }

    void showUser(String userId) {
        Bundle b = new Bundle();
        b.putString(Cache.USER_ID, userId);
        b.putString("phone", "");
        startActivity(PersonalHomePageActivity.class, b);
    }

    private ShareDetailsBean msg;

    @Override
    public void onSuccess(ShareDetailsBean msg) {
        this.msg = msg;
        getBasicView();
        if (msg.getData() != null && msg.getData().getPersoninfo().size() > 0) {
            if (onRefreshJudge) {
//                Log.e(TAG, "onRefreshJudge: " + onRefreshJudge);
                myShareDetailsAdapter.setNewData(msg.getData().getPersoninfo());
                mSwipeRefreshLayout.setRefreshing(false);
                myShareDetailsAdapter.setEnableLoadMore(true);
                onRefreshJudge = false;
            }
        } else {
            mSwipeRefreshLayout.setRefreshing(false);
            myShareDetailsAdapter.setEnableLoadMore(true);
            onRefreshJudge = false;
        }
    }

    @Override
    public void onShowError(String msg) {

    }

    @Override
    public void onRefresh() {
        onRefreshJudge = true;
        myShareDetailsPresenter.initShareDetails(getIntent().getExtras().getString("bookId"));
    }

    @Override
    public void onLoadMoreRequested() {
        myShareDetailsAdapter.loadMoreEnd(true);
    }
}
